package com.lyd.productanalyse.service.impl;

import com.lyd.productanalyse.entity.EchartResultDomain;
import com.lyd.productanalyse.entity.ExcelCalResultDomain;
import com.lyd.productanalyse.service.FilterDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * 描述 过滤数据实现类
 *
 * @author liyadong
 * @created 2023-11-20 15:50
 */
@Service
public class FilterDataServiceImpl implements FilterDataService {
    /**
     * 日志打印
     */
    private final static Logger logger = LoggerFactory.getLogger(FilterDataServiceImpl.class);
    /**
     * 过滤数据
     *
     * @param rate                  过滤比例
     * @param excelCalResultDomain  原始数据
     * @return 过滤后数据
     */
    @Override
    public Map<String, BigDecimal> filter(String rate, ExcelCalResultDomain excelCalResultDomain) {
        BigDecimal total = excelCalResultDomain.getTotal();
        // 根据比例计算过滤限制
        BigDecimal filterLimit = total.multiply(new BigDecimal(rate));
        // 原始数据
        Map<String, BigDecimal> sortProductResultMap = excelCalResultDomain.getSortProductResultMap();
        Map<String, BigDecimal> filterSortProductResultMap = new LinkedHashMap<>();
        BigDecimal currentCalResult = BigDecimal.ZERO;
        logger.info("开始过滤数据");
        for (Map.Entry<String, BigDecimal> entry : sortProductResultMap.entrySet()) {
            currentCalResult = currentCalResult.add(entry.getValue());
            // 如果当前累积结果加上当前值小于过滤限制，则移除该键值对
            if(currentCalResult.compareTo(filterLimit) <= 0){
                filterSortProductResultMap.put(entry.getKey(), entry.getValue());
            }
        }
        return filterSortProductResultMap;
    }

    @Override
    public EchartResultDomain handleData(String percent, BigDecimal total, Map<String, BigDecimal> filter) {
        EchartResultDomain echartResultDomain = new EchartResultDomain();

        // 总额 * 过滤比例
        BigDecimal rateValue = BigDecimal.ZERO;
        if (total != null && StringUtils.hasText(percent)) {
            rateValue = total.multiply(new BigDecimal(percent));
        }

        // X轴数据
        List<String> xAxisList = new LinkedList<>();
        // y轴数据
        List<BigDecimal> yAxisList = new LinkedList<>();
        filter.forEach((key, value) -> {
            xAxisList.add(key);
            yAxisList.add(value);
        });

        // 计算差值(渲染表格用)
        List<BigDecimal> differenceAxis = new ArrayList<>();
        BigDecimal calRateValue = rateValue;
        for (BigDecimal yValue : yAxisList) {
            calRateValue = calRateValue.subtract(yValue);
            differenceAxis.add(calRateValue);
        }

        // 添加第一条数据
        BigDecimal multiply = new BigDecimal(percent).multiply(new BigDecimal("100"));
        xAxisList.add(0, "总额" + multiply.toPlainString() + "%");
        // 添加第一条数据
        yAxisList.add(0, rateValue);
        // 添加第一条数据
        differenceAxis.add(0, BigDecimal.ZERO);

        // 赋值对象
        echartResultDomain.setPercent(percent);
        echartResultDomain.setXAxis(xAxisList);
        echartResultDomain.setYAxis(yAxisList);
        echartResultDomain.setTotal(total);
        echartResultDomain.setRateValue(rateValue);
        echartResultDomain.setDifferenceAxis(differenceAxis);
        return echartResultDomain;
    }

}
